package com.dmooo.ymx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.dmooo.ymx.R;
import com.dmooo.ymx.adapter.XuanpinkAdapter;
import com.dmooo.ymx.base.BaseActivity;
import com.dmooo.ymx.bean.Xuanpinkbean;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
         * 选品库
         * */
public class XuanpinkActivity extends BaseActivity {

    private LinearLayout ly_back;
    private GridView recyclerView;
    public int page = 1;

    private SmartRefreshLayout smartRefreshLayout;
    private XuanpinkAdapter xuanpinkAdapter;
    @Override
    protected void initUI() {
        setContentView( R.layout.activity_xuanpink );

        ly_back = findViewById( R.id.xuanpink_lyback );
        recyclerView = findViewById( R.id.xuanpink_recycler );
        smartRefreshLayout = findViewById( R.id.refresh_layout );
        ly_back.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        } );
    }

    @Override
    protected void initData() {
        xuanpinkAdapter = new XuanpinkAdapter( XuanpinkActivity.this,R.layout.today_highlights_child_item2,xuanpinkbeans );
        recyclerView.setAdapter( xuanpinkAdapter );
        recyclerView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString("num_iid", xuanpinkbeans.get( i ).num_iid);
                Intent intent = new Intent(XuanpinkActivity.this, PromotionDetailsActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        } );

        getTbkListRequst(1);

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshlayout) {
                xuanpinkbeans.clear();
                page = 1;
                getTbkListRequst(page);
                //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                getTbkListRequst(page);
                //refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败

            }
        });
    }

    @Override
    protected void initListener() {

    }

    public List<Xuanpinkbean> xuanpinkbeans = new ArrayList<>(  );
    private  void getTbkListRequst(int p) {
        RequestParams requestParams = new RequestParams();
        requestParams.put("favorites_id", Constants.XUANPIN_ID);
        requestParams.put("p", p);
        requestParams.put("per", "8");
        HttpUtils.post( Constants.XUANPINKU, requestParams, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                showToast(throwable.getMessage());
//                Log.e( "statuscode",search_content+","+ responseString+","+throwable.getMessage());
            }

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishRefresh();
                    smartRefreshLayout.finishLoadMore();
                }
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("ssdaafsdf",responseString);
                try {
                    JSONObject object = new JSONObject(responseString);
                    if ("0".equals(object.getString("code"))) {
                        JSONObject data = object.getJSONObject( "data" );
                        JSONArray array = data.getJSONArray("list");

                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = (JSONObject) array.get( i );
                            Xuanpinkbean xuanpinkbean = new Xuanpinkbean();
                            xuanpinkbean.num_iid = obj.getString( "num_iid" );
                            xuanpinkbean.title = obj.getString( "title" );
                            xuanpinkbean.pict_url = obj.getString( "pict_url" );
//                            xuanpinkbean.small_images = obj.getString( "small_images" );
//                            xuanpinkbean.reserve_price = obj.getString( "reserve_price" );
                            xuanpinkbean.zk_final_price = obj.getString( "zk_final_price" );
//                            xuanpinkbean.user_type = obj.getString( "user_type" );
//                            xuanpinkbean.provcity = obj.getString( "provcity" );
//                            xuanpinkbean.item_url = obj.getString( "item_url" );
//                            xuanpinkbean.click_url = obj.getString( "click_url" );
//                            xuanpinkbean.nick = obj.getString( "nick" );
//                            xuanpinkbean.seller_id = obj.getString( "seller_id" );
                            xuanpinkbean.volume = obj.getString( "volume" );
                            xuanpinkbean.tk_rate = obj.getString( "tk_rate" );
//                            xuanpinkbean.zk_final_price_wap = obj.getString( "zk_final_price_wap" );
//                            xuanpinkbean.shop_title = obj.getString( "shop_title" );
//                            xuanpinkbean.event_start_time = obj.getString( "event_start_time" );
//                            xuanpinkbean.event_end_time = obj.getString( "event_end_time" );
//                            xuanpinkbean.type = obj.getString( "type" );
//                            xuanpinkbean.status = obj.getString( "status" );
//                            xuanpinkbean.category = obj.getString( "category" );
//                            xuanpinkbean.coupon_click_url = obj.getString( "coupon_click_url" );
//                            xuanpinkbean.coupon_end_time = obj.getString( "coupon_end_time" );
//                            xuanpinkbean.coupon_info = obj.getString( "coupon_info" );
                            xuanpinkbean.coupon_amount = obj.getString( "coupon_amount" );
//                            xuanpinkbean.coupon_start_time = obj.getString( "coupon_start_time" );
//                            xuanpinkbean.coupon_total_count = obj.getString( "coupon_total_count" );
//                            xuanpinkbean.coupon_remain_count = obj.getString( "coupon_remain_count" );
                            xuanpinkbeans.add( xuanpinkbean );
//                            xuanpinkbeans.add(new Gson().fromJson(array.getJSONObject(i).toString(), Xuanpinkbean.class));
                        }
                        xuanpinkAdapter.notifyDataSetChanged();
                    } else {
                        showToast(object.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        });
    }

}
